<?php
/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property integer $userID
 * @property string $first_name
 * @property string $last_name
 * @property string $user_name
 * @property string $emailID
 * @property string $user_type
 * @property string $password
 * @property integer $referenceID
 * @property integer $user_cityID
 * @property integer $user_stateID
 * @property string $activation_key
 * @property integer $total_points
 * @property string $profile_image
 * @property string $college_id_image
 * @property string $qualification
 * @property string $address
 * @property string $mobile_number
 * @property string $birth_date
 * @property string $gender
 * @property string $payment_status
 * @property string $payment_type
 * @property string $created
 * @property string $modified
 *
 * The followings are the available model relations:
 * @property CompetitionAnswer[] $competitionAnswers
 * @property DesiredJob[] $desiredJobs
 * @property Document[] $documents
 * @property EduAddCourse[] $eduAddCourses
 * @property EduHigherQual[] $eduHigherQuals
 * @property EduJrcollege[] $eduJrcolleges
 * @property EduSchool[] $eduSchools
 * @property Employer[] $employers
 * @property ExtraActivity[] $extraActivities
 * @property Image[] $images
 * @property JobApplication[] $jobApplications
 * @property JobOtherDetails[] $jobOtherDetails
 * @property Likes[] $likes
 * @property OfferSuggestion[] $offerSuggestions
 * @property PollAnswer[] $pollAnswers
 * @property ProjDetail[] $projDetails
 * @property SeDiscountJob[] $seDiscountJobs
 * @property ShareEarn[] $shareEarns
 * @property Subscription[] $subscriptions
 * @property SurveyAnswer[] $surveyAnswers
 * @property TransactionDetail[] $transactionDetails
 * @property City $userCity
 * @property State $userState
 * @property Video[] $videos
 * @property WorkExp[] $workExps
 */
class User extends CActiveRecord
{
	public $verifyCode;
	public $email;
	public $iAgree;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('first_name, last_name, user_name, emailID, password, gender, birth_date, mobile_number, verifyCode', 'required','on'=>array('register')),
			array('referenceID, user_cityID, user_stateID, total_points, mobile_number', 'numerical', 'integerOnly'=>true),
			array('verifyCode, iAgree', 'required','on'=>array('yxRegister')),
			array('first_name, last_name, user_name, user_type, password, activation_key, profile_image, college_id_image, qualification, address, payment_status, payment_type', 'length', 'max'=>100),
			array('emailID', 'length', 'max'=>200),
			array('mobile_number', 'length', 'max'=>20),
			array('gender', 'length', 'max'=>45),
			array('emailID', 'email','on'=>array('register')),
			array('emailID', 'unique','on'=>array('register')),
			array('user_name', 'unique','on'=>array('register')),
			array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd'),'on'=>'register'),
			array('birth_date, created, modified', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.

			array('userID, first_name, last_name, user_name, emailID, user_type, password, referenceID, user_cityID, user_stateID, activation_key, total_points, profile_image, college_id_image, qualification, address, mobile_number, birth_date, gender, payment_status, payment_type, created, modified', 'safe', 'on'=>'search'),

			array('modified','default',
           	  'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'update'),
        	array('created,modified','default',
              'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'insert'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'competitionAnswers' => array(self::HAS_MANY, 'CompetitionAnswer', 'userID'),
			'desiredJobs' => array(self::HAS_MANY, 'DesiredJob', 'userID'),
			'documents' => array(self::HAS_MANY, 'Document', 'userID'),
			'eduAddCourses' => array(self::HAS_MANY, 'EduAddCourse', 'userID'),
			'eduHigherQuals' => array(self::HAS_MANY, 'EduHigherQual', 'userID'),
			'eduJrcolleges' => array(self::HAS_MANY, 'EduJrcollege', 'userID'),
			'eduSchools' => array(self::HAS_MANY, 'EduSchool', 'userID'),
			'employers' => array(self::HAS_MANY, 'Employer', 'userID'),
			'extraActivities' => array(self::HAS_MANY, 'ExtraActivity', 'userID'),
			'images' => array(self::HAS_MANY, 'Image', 'userID'),
			'jobApplications' => array(self::HAS_MANY, 'JobApplication', 'applicantID'),
			'jobOtherDetails' => array(self::HAS_MANY, 'JobOtherDetails', 'userID'),
			'likes' => array(self::HAS_MANY, 'Likes', 'userID'),
			'offerSuggestions' => array(self::HAS_MANY, 'OfferSuggestion', 'userID'),
			'pollAnswers' => array(self::HAS_MANY, 'PollAnswer', 'userID'),
			'projDetails' => array(self::HAS_MANY, 'ProjDetail', 'userID'),
			'seDiscountJobs' => array(self::HAS_MANY, 'SeDiscountJob', 'userID'),
			'shareEarns' => array(self::HAS_MANY, 'ShareEarn', 'userID'),
			'subscriptions' => array(self::HAS_MANY, 'Subscription', 'userID'),
			'surveyAnswers' => array(self::HAS_MANY, 'SurveyAnswer', 'userID'),
			'transactionDetails' => array(self::HAS_MANY, 'TransactionDetail', 'userID'),
			'userCity' => array(self::BELONGS_TO, 'City', 'user_cityID'),
			'userState' => array(self::BELONGS_TO, 'State', 'user_stateID'),
			'videos' => array(self::HAS_MANY, 'Video', 'userID'),
			'workExps' => array(self::HAS_MANY, 'WorkExp', 'userID'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'userID' => 'User',
			'first_name' => 'First Name',
			'last_name' => 'Last Name',
			'user_name' => 'User Name',
			'emailID' => 'Email',
			'user_type' => 'User Type',
			'password' => 'Password',
			'referenceID' => 'referenceID',
			'user_cityID' => 'User City',
			'user_stateID' => 'User State',
			'activation_key' => 'Activation Key',
			'total_points' => 'Total Points',
			'profile_image' => 'Profile Image',
			'college_id_image' => 'College Id Image',
			'qualification' => 'Qualification',
			'address' => 'Address',
			'mobile_number' => 'Mobile Number',
			'birth_date' => 'Birth Date',
			'gender' => 'Gender',
			'payment_status' => 'Payment Status',
			'payment_type' => 'Payment Type',
			'created' => 'Created',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('userID',$this->userID);
		$criteria->compare('first_name',$this->first_name,true);
		$criteria->compare('last_name',$this->last_name,true);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('emailID',$this->emailID,true);
		$criteria->compare('user_type',$this->user_type,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('referenceID',$this->referenceID);
		$criteria->compare('user_cityID',$this->user_cityID);
		$criteria->compare('user_stateID',$this->user_stateID);
		$criteria->compare('activation_key',$this->activation_key,true);
		$criteria->compare('total_points',$this->total_points);
		$criteria->compare('profile_image',$this->profile_image,true);
		$criteria->compare('college_id_image',$this->college_id_image,true);
		$criteria->compare('qualification',$this->qualification,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('mobile_number',$this->mobile_number,true);
		$criteria->compare('birth_date',$this->birth_date,true);
		$criteria->compare('gender',$this->gender,true);
		$criteria->compare('payment_status',$this->payment_status,true);
		$criteria->compare('payment_type',$this->payment_type,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getModel($type,$arrParams = array())
	{
		switch ($type)
		{
			case 'USER-ID':
				//On the basis of stateID it will return city list
				$objUser = User::model()->find('userID=?',array($arrParams['userID']));
			return $objUser;
			break;
			
			case 'USER-SEARCH_PARAMS':
				if($arrParams['search_param']=='name')
				{
				$match = addcslashes($arrParams['first_name'], '%_'); // escape LIKE's special characters
				$objUser = User::model()->find( 'userID = :userID AND (first_name LIKE :match OR last_name LIKE :match)', array(':userID'=>$arrParams['userID'],':match' => "%$match%"));
				}
				elseif($arrParams['search_param']=='job_title')
				{
				$match = addcslashes($arrParams['job_title'], '%_'); // escape LIKE's special characters
				$objUser = Job::model()->find( 'jobID = :jobID AND job_title LIKE :match', array(':jobID'=>$arrParams['jobID'],':match' => "%$match%"));
				}
				elseif($arrParams['search_param']=='job_type')
				{

				$objUser = Job::model()->find( 'jobID = :jobID AND job_type = :job_type', array(':jobID'=>$arrParams['jobID'],':job_type' => $arrParams['job_type']));
				}
				elseif($arrParams['search_param']=='work_exp')
				{
				$objUser = WorkExp::model()->find( 'userID = :userID AND experience_yr = :experience_yr', array(':userID'=>$arrParams['userID'],':experience_yr' => floor($arrParams['work_exp'])));
				}
				elseif($arrParams['search_param']=='qualification')
				{
				$match = addcslashes($arrParams['qualification'], '%_'); // escape LIKE's special characters
				$objUser = EduHigherQual::model()->find( 'userID = :userID AND course_name LIKE :match', array(':userID'=>$arrParams['userID'],':match' => "%$match%"));
				}
				elseif($arrParams['search_param']=='skill')
				{
				$match = addcslashes($arrParams['skill'], '%_'); // escape LIKE's special characters
				$objUser = WorkExp::model()->find( 'userID = :userID AND skills LIKE :match', array(':userID'=>$arrParams['userID'],':match' => "%$match%"));
				}
			return $objUser;
			break;
		
			case 'ACTIVATION-KEY':
				$objUser = User::model()->find('activation_key=?',array($arrParams['activation_key']));
			return $objUser;
			break;
			
		}
	}
	
	public function getUserJobProfile($userID)
	{
		$totalExperience = "";
		$qualificationdetails = "";
		$skills = "";
		$userWorkExp = WorkExp::model()->getModel('USER-ID',array('userID'=>$userID));
		if ($userWorkExp)
		{
			$totalExperience = "Experience: ".$userWorkExp->experience_yr." Year ".$userWorkExp->experience_month." Months";
			$skills = "Skills: ".$userWorkExp->skills;
			
		}
		
		$qualification = EduHigherQual::model()->getModel('USER-ID',array('userID'=>$userID));
		if($qualification)
		{
			$qualificationdetails = $qualification->course_name.", ".$qualification->year_passing;
		
		}
		return $totalExperience." ".$skills." ".$qualificationdetails;
	}
}